package com.diqye.controller.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.diqye.db.model.XModel;

public class CKit {
	public static LinkedList<XModel> msglist = new LinkedList<XModel>();
	public static XModel getLogin(HttpServletRequest request){
		return (XModel) request.getSession().getAttribute("login");
	}
	
	public static XModel getNoLogin(HttpServletRequest request){
		return (XModel) request.getSession().getAttribute("nologin");
	}
	
	public static String getLoginQQ(HttpServletRequest request){
		return getLogin(request).gts("qq");
	}
	
	public static String getLoginSname(HttpServletRequest request){
		return getLogin(request).gts("sname");
	}
	public static boolean isLogin(HttpServletRequest request){
		return getLogin(request)!=null;
	}
	
	public static String getCurrentDate(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
	}
	
	public static String getCurrentMonth(){
		return new SimpleDateFormat("yyyy-MM-00").format(new Date());
	}
	
	public static String getLastMonth(){
		Date d=new Date();
		d.setMonth(d.getMonth()-1);
		return new SimpleDateFormat("yyyy-MM-00").format(d);
	}
	
	public static String and(String where,String sql){
		if(!notEmpty(where)){
			where =" where "+sql;
		}else{
			where +=" and "+sql;
		}
		return where;
	}
	public static boolean notEmpty(String s){
		return s!=null&&!s.trim().equals("");
	}
	public static XModel list(long count,List list){
		return new XModel().set("count", count).s("list", list);
	}
	
	public static List<XModel> getMsgList(){
		return msglist;
	}
	
	public static void addmsg(XModel msg){
		msglist.add(msg);
		if(msglist.size()>10){
			msglist.removeFirst();
		}
	}
	
}
